package netty.io.demo.NIO;

import netty.io.demo.NIO.handle.NIOClientHandle;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.util.Scanner;

public class NIOClient {
  private static NIOClientHandle clientHandle;
  
  public static void start() throws IOException {
    if (clientHandle != null) {
      clientHandle.stop();
    }
    
    clientHandle = new NIOClientHandle(new InetSocketAddress("127.0.0.1", 10001));
    new Thread(clientHandle, "client").start();
  }
  
  /**
   * 发送消息的 api
   *
   * @param message 消息内容
   * @throws IOException 异常报错
   * @author lihh
   */
  public static void sendMessage(String message) throws IOException {
    clientHandle.sendMessage(message);
  }
  
  public static void main(String[] args) throws IOException {
    // 这里 启动单线程 执行nio
    start();
    Scanner scanner = new Scanner(System.in);
    while (true) {
      NIOClient.sendMessage(scanner.next());
    }
  }
}
